package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Notice;
import cn.wolfcode.domain.UnreadNotice;
import cn.wolfcode.enums.NoticeStatusEnum;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IEmployeeService employeeService;


    //@RequiredPermission(name = "公告页面", expression = "notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo) {

        Long empId = UserContext.getCurrentUser().getId();

        UnreadNotice unreadNotice = new UnreadNotice();
        //查询出所有未读的公告,设置进去对象里面
        unreadNotice.setNotices(noticeService.queryUnreadNotices(empId));
        //查询当前用户未读公告的条数,设置进去对象里面
        unreadNotice.setSize(noticeService.queryUnreadSize(empId));
        //设置未读的公告 , 把封装好的数据放到session中去
        UserContext.setUnreadNotice(unreadNotice);


        PageInfo<Notice> pageInfo = noticeService.query(empId, qo);
        model.addAttribute("pageInfo", pageInfo);
        NoticeStatusEnum[] values = NoticeStatusEnum.values();
        model.addAttribute("enums", values);
        return "/notice/list";
    }


    @RequiredPermission(name = "公告删除", expression = "notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name = "公告新增/编辑", expression = "notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "notice/list";
    }


    @RequiredPermission(name = "公告新增/编辑", expression = "notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice", noticeService.get(id));
        }
        return "notice/input";
    }

    //@RequiredPermission(name = "公告新增/编辑", expression = "notice:saveOrUpdate")
    @RequestMapping("/show")
    public String show(Long id, Model model) {
        noticeService.changeReadStatus(id);
        model.addAttribute("content", noticeService.get(id));

        Long empId = UserContext.getCurrentUser().getId();

        UnreadNotice unreadNotice = new UnreadNotice();
        //查询出所有未读的公告,设置进去对象里面
        unreadNotice.setNotices(noticeService.queryUnreadNotices(empId));
        //查询当前用户未读公告的条数,设置进去对象里面
        unreadNotice.setSize(noticeService.queryUnreadSize(empId));
        //设置未读的公告 , 把封装好的数据放到session中去
        UserContext.setUnreadNotice(unreadNotice);
        return "notice/show";
    }

    @RequestMapping("/release")
    public String pushOrRemove(Long id, Model model) {
        noticeService.updateStatus(id);
        return "redirect:/notice/list";
    }
}
